package com.weigee.ktv.v.adapter.grid;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.l.d.app.library.adapter.MyListAdapter;
import com.l.d.app.library.eventbus.EventSend;
import com.weigee.ktv.R;
import com.weigee.ktv.m.EventTag;
import com.weigee.ktv.m.entity.WrapHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import skin.support.widget.SkinCompatTextView;

/**
 * Created by linde on 2017/04/24.
 * <p/>
 */
public final class RechargeReturnAdapter extends MyListAdapter<JSONObject, RechargeReturnAdapter.Holder> {
    private int m_iCheckItem;

    public RechargeReturnAdapter() {
        m_iCheckItem = -1;
    }

    @NonNull
    @Override
    protected List<JSONObject> onCreateNewEmptyList() {
        return new ArrayList<>();
    }

    @NonNull
    @Override
    protected Holder onCreateViewHolder(int position, ViewGroup parent) {
        final Context context = parent.getContext();
        final View itemView = LayoutInflater.from(context).inflate(R.layout.rechargereturn_frag___gv_item, parent, false);
        return new Holder(itemView);
    }

    @Override
    protected void onBindViewHolder(Holder holder, final int position) {
        final JSONObject object = getItem(position);
        if (object == null) {
            return;
        }
        final int recharge = object.getIntValue("recharge");
        final int rechargeReturn = object.getIntValue("return");

        final boolean isSelected = m_iCheckItem == position;
        holder.group.setSelected(isSelected);
        holder.tvRechargeTip.setSelected(isSelected);
        holder.tvRechargeReturnTip.setSelected(isSelected);

        holder.tvRechargeTip.setText(String.valueOf(recharge + "元"));
        holder.tvRechargeReturnTip.setText(String.valueOf("赠送:" + rechargeReturn + "元"));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (m_iCheckItem == position) {
                    m_iCheckItem = -1;
                } else {
                    m_iCheckItem = position;
                }
                EventSend.with(EventTag.EVENT_RECHARGE_RETURN_CHECK_AMOUNT_RETURN)
                        .msg(m_iCheckItem)
                        .build();
                notifyDataSetChanged();
            }
        });
    }

    public void clearAmountChecked() {
        m_iCheckItem = -1;
        notifyDataSetChanged();
    }

    public int getSelectedAmount() {
        final JSONObject object = getItem(m_iCheckItem);
        if (object == null || object.isEmpty()) {
            return 0;
        }
        final int recharge = object.getIntValue("recharge");
        final int rechargeReturn = object.getIntValue("return");
        return recharge + rechargeReturn;
    }

    // 获取充值(如果)后的返利金额
    public int getAmountReturnIfRecharge(final int recharge) {
        JSONObject object;
        int objRecharge;
        int iReturn = 0;// 充值后（如果）返利的金额
        for (int i = 0, size = getCount(); i < size; i++) {
            object = getItem(i);
            if (object == null) continue;
            objRecharge = object.getIntValue("recharge");
            if (recharge >= objRecharge) {
                iReturn = object.getIntValue("return");
            } else {
                return iReturn;
            }
        }
        return iReturn;
    }

    static class Holder extends WrapHolder {
        @BindView(R.id.group_recharge_return_item)
        ViewGroup group;
        @BindView(R.id.tv_recharge_tip)
        SkinCompatTextView tvRechargeTip;
        @BindView(R.id.tv_recharge_return_tip)
        SkinCompatTextView tvRechargeReturnTip;

        private Holder(View itemView) {
            super(itemView);
        }
    }
}
